package com.ewili.x.special.task.thread.impl;

import com.ewili.framework.utils.HtmlUnitUtils;
import com.ewili.framework.utils.StringUtils;
import com.ewili.x.special.dao.inter.HyRankDao;
import com.ewili.x.special.entity.HyRank;
import com.ewili.x.special.task.thread.inter.HyRankThreadTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class HyRankThreadTaskImpl implements HyRankThreadTask {

    @Autowired
    private HyRankDao hyRankDao;

    //@Async("myTaskAsyncPool")
    @Override
    public void spideData(String url, String stockCode, Date currentDate) {
        String content = HtmlUnitUtils.getContent(url);
        Document document = Jsoup.parse(content);
        Element element = document.getElementById("cwzbDataBox");
        List<Element> trs = element.getElementsByTag("tr");

        HyRank hyRank = hyRankDao.findByStockCodeAndDate(stockCode, currentDate);

        if(hyRank == null) {
            hyRank = new HyRank();
        }
        hyRank.setStockCode(stockCode);
        hyRank.setDate(currentDate);

        for (Element ele : trs) {
            if(ele.toString().contains("行业平均")) {
                List<Element> sockers =  ele.getElementsByTag("td");
                //行业代码
                String bkCode = StringUtils.substring(sockers.get(0).toString(),"#boards-BK","\" target=\"_blank\"").replace("#boards-BK","");
                //总市值
                String totalValueAvg = sockers.get(1).text().replace("亿","").replace("万","").trim();
                //净资产
                String netAssetAvg = sockers.get(2).text().replace("亿","").replace("万","").trim();
                //净利润
                String netProfitsAvg = sockers.get(3).text().replace("亿","").replace("万","").trim();
                //市盈率
                String peAvg = sockers.get(4).text();
                //市净率
                String pbAvg = sockers.get(5).text();
                //毛利率
                String grossProfitRatioAvg = sockers.get(6).text().replace("%","").trim();
                //净利率
                String netProfitMarginAvg = sockers.get(7).text().replace("%","").trim();
                //roe
                String roeAvg = sockers.get(8).text().replace("%","").trim();



                hyRank.setBkCode(bkCode);
                hyRank.setTotalValueAvg(Double.valueOf(totalValueAvg));
                hyRank.setNetAssetAvg(Double.valueOf(netAssetAvg));
                hyRank.setNetProfitsAvg(Double.valueOf(netProfitsAvg));
                hyRank.setPeAvg(Double.valueOf(peAvg));
                hyRank.setPbAvg(Double.valueOf(pbAvg));
                hyRank.setGrossProfitRatioAvg(Double.valueOf(grossProfitRatioAvg));
                hyRank.setNetProfitMarginAvg(Double.valueOf(netProfitMarginAvg));
                hyRank.setRoeAvg(Double.valueOf(roeAvg));

                //System.out.println(bkCode + " " + totalValue + " " + netAsset + " " + netProfits + " " + pe + " " + pb + " " + grossProfitRatio + " "+ netProfitMargin + " " + roe);
            } else if (ele.toString().contains("<b> 行业排名 </b>")) {
                String range = ele.text().replace("行业排名","").trim();
                String[] rangeArr = range.split(" ");
                String totalValueRange = rangeArr[0].split("\\|")[0];
                String netAssetRange = rangeArr[1].split("\\|")[0];
                String netProfitsRange = rangeArr[2].split("\\|")[0];
                String peRange = rangeArr[3].split("\\|")[0].trim();
                if("-".equals(peRange)) {
                    peRange = "0";
                }
                String pbRange = rangeArr[4].split("\\|")[0];
                String grossProfitRatioRange = rangeArr[5].split("\\|")[0];
                String netProfitMarginRange = rangeArr[6].split("\\|")[0];
                String roeRange = rangeArr[7].split("\\|")[0];
                String totalCompanies = rangeArr[7].split("\\|")[1];
                //特殊字符 -
                hyRank.setTotalValueRange(Double.valueOf(totalValueRange));
                hyRank.setNetAssetRange(Double.valueOf(netAssetRange));
                hyRank.setNetProfitsRange(Double.valueOf(netProfitsRange));
                hyRank.setPeRange(Double.valueOf(peRange));
                hyRank.setPbRange(Double.valueOf(pbRange));
                hyRank.setGrossProfitRatioRange(Double.valueOf(grossProfitRatioRange));
                hyRank.setNetProfitMarginRange(Double.valueOf(netProfitMarginRange));
                hyRank.setRoeRange(Double.valueOf(roeRange));
                hyRank.setTotalCompanies(Double.valueOf(totalCompanies));
                //System.out.println(totalValueRange + " " + netAssetRange + " " + netProfitsRange + " " + peRange + " " + pbRange + " " + grossProfitRatioRange + " "+ netProfitMarginRange + " " + roeRange);
            } else if (ele.toString().contains("四分位属性")) {
                //特殊字符 - -
                List<Element> sockers = ele.getElementsByTag("p");
                String totalValueRank = sockers.get(0).text();
                String netAssetRank = sockers.get(1).text();
                String netProfitsRank = sockers.get(2).text();
                String peRank = sockers.get(3).text().trim();
                if("- -".equals(peRank)){
                    peRank = "0";
                }
                String pbRank = sockers.get(4).text();
                String grossProfitRatioRank = sockers.get(5).text();
                String netProfitMarginRank = sockers.get(6).text();
                String roeRank = sockers.get(7).text();

                hyRank.setTotalValueRank(totalValueRank);
                hyRank.setPbRank(pbRank);
                hyRank.setGrossProfitRatioRank(grossProfitRatioRank);
                hyRank.setNetProfitMarginRank(netProfitMarginRank);
                hyRank.setNetAssetRank(netAssetRank);
                hyRank.setPeRank(peRank);
                hyRank.setNetProfitsRank(netProfitsRank);
                hyRank.setRoeRank(roeRank);
                //System.out.println(totalValueRank + " " + netAssetRank + " " + netProfitsRank + " " + peRank + " " + pbRank + " " + grossProfitRatioRank + " "+ netProfitMarginRank + " " + roeRank);
            } else {
                List<Element> sockers = ele.getElementsByTag("td");
                String totalValue = sockers.get(1).text().replace("亿","").replace("万","").trim();
                String netAsset = sockers.get(2).text().replace("亿","").replace("万","").trim();
                String netProfits = sockers.get(3).text().replace("亿","").replace("万","").trim();
                String pe = sockers.get(4).text().trim();
                if("负值".equals(pe)){
                    pe = "0";
                }
                String pb = sockers.get(5).text();
                String grossProfitRatio = sockers.get(6).text().replace("%","").trim();
                String netProfitMargin = sockers.get(7).text().replace("%","").trim();
                String roe = sockers.get(8).text().replace("%","").trim();

                hyRank.setTotalValue(Double.valueOf(totalValue));
                hyRank.setNetAsset(Double.valueOf(netAsset));
                hyRank.setNetProfits(Double.valueOf(netProfits));
                hyRank.setPe(Double.valueOf(pe));
                hyRank.setPb(Double.valueOf(pb));
                hyRank.setGrossProfitRatio(Double.valueOf(grossProfitRatio));
                hyRank.setNetProfitMargin(Double.valueOf(netProfitMargin));
                hyRank.setRoe(Double.valueOf(roe));
            }
        }

        hyRankDao.save(hyRank);

    }
}
